/**
 * @description 数据键值对-控制器
 * @since 2019-11-12
 * @author Rid King
*/

const ox = require('@daelui/oxjs')
const { dater }= require('@daelui/oxkit')
const BaseController = require('../base/index.js')
const Service = require('./service.js')

class Controller extends BaseController {
  /**
   * @function 构造方法
  */
  constructor (args) {
    super(args)
    // 服务
    this.$service = new Service(args)
  }

  // 首页
  index (action) {
    // 解析数据
    return this.queryNotes(action).then(async res => {
      let params = action.params || {}
      let data = res.data || {}
      return this.renderTpl('/note/index.ejs', {
        data: {
          pager: data.pager,
          notes: data.list,
          params: params
        }
      }).then(tpl => {
        return {
          data: tpl
        }
      })
    })
  }

  // 查询日记
  queryNotes (action) {
    // 解析数据
    return this.solveAction(action).then(async action => {
      let params = action.params || {}
      action.params = params
      // 解析数据
      return this.queryAllByPage(action, {
        excute: {
          operators: [
            {field: 'title', operator: 'LIKE'},
            {field: 'type', operator: 'EQUAL'},
            {field: 'status', operator: 'EQUAL'},
            {field: 'labels', operator: 'LIKE'},
            {field: 'content', operator: 'LIKE'},
            {field: 'createTime', operator: 'BETWEEN'},
            {field: 'updateTime', operator: 'BETWEEN'}
          ],
          limit: {
            pageSize: {field: 'pageSize', default: 10},
            pageIndex: {field: 'pageIndex', default: 1}
          },
          sort: [
            {field: 'createTime', by: 'desc'},
            {field: 'orders', by: 'desc'}
          ]
        },
        params: {
          status: 1
        }
      }).then(async res => {
        let data = res.data || {}
        let notes = data.list
        notes = Array.isArray(notes) ? notes : []
        notes.forEach(item => {
          item.content = this.getHtmlText(item.content) + '...'
          item.createTime = dater.format(item.createTime)
        })
        let pager = data.pager || { pageIndex: 1, pageSize: 10, total: 0, pageCount: 0 }
        return {
          data: {
            pager: pager,
            list: notes
          }
        }
      })
    })
  }

  // 获取纯文本
  getHtmlText(val) {
    if (val != null && val != "") {
      var msg = val.replace(/<[^>]+>|&[^>]+;/g, ''); //去除HTML Tag
      msg = msg.replace(/[|]*n/, '') //去除行尾空格
      msg = msg.replace(/&npsp;/ig, ''); //去掉npsp
      return msg.substr(0, 150) // 获文本文字内容的前100个字符
    } else {
      return ''
    }
  }

  // 详情
  detail (action) {
    // 解析数据
    return this.queryNoteItem(action).then(async res => {
      let params = action.params || {}
      let data = res.data || {}
      return this.renderTpl('/note/detail.ejs', {
        data: {
          note: data,
          params: params
        }
      }).then(tpl => {
        return {
          data: tpl
        }
      })
    })
  }

  // 查询日记
  queryNoteItem (action) {
    // 解析数据
    return this.solveAction(action).then(async action => {
      let params = action.params || {}
      action.params = params
      // 解析数据
      return this.query(action, {
        excute: {
          operators: [
            {field: 'id', operator: 'EQUAL'},
            {field: 'status', operator: 'EQUAL'}
          ]
        },
        params: {
          status: 1
        }
      }).then(async res => {
        let data = res.data || {}
        let node = Object.assign({}, data)
        if (node.id) {
          let viewCount = parseInt(node.viewCount)
          viewCount = isNaN(viewCount) ? 0 : viewCount
          viewCount += 1
          node.viewCount = viewCount
          action.params = node
          action.excute = {
            operators: [
              {field: 'id', operator: 'EQUAL'}
            ]
          }
          this.$service.update(action)
        }
        data.createTime = dater.format(data.createTime)
        return {
          data: data
        }
      })
    })
  }

  // 查询类型
  queryTypes () {
    // 查询所有的文档数据
    return this.$router.getControllInstance({path: '/dblog/noteType'}).queryAll({}, {
      action: {},
      query: {},
      params: {
        status: 1
      },
      excute: {
        operators: [
          {field: 'status', operator: 'EQUAL'}
        ],
        sort: [
          {field: 'createTime', by: 'desc'},
          {field: 'orders', by: 'desc'}
        ]
      }
    })
  }
}

module.exports = Controller